//
//  HYFTabBar.m
//  MyOCWeibo
//
//  Created by 黄勇锋 on 16/10/3.
//  Copyright © 2016年 黄勇锋. All rights reserved.
//

#import "HYFTabBar.h"

@interface HYFTabBar()

@property (weak, nonatomic) UIButton *plusButton;

@end


@implementation HYFTabBar

//编译器认为我们自定义的属性 和父类的重名,是要实现自己的逻辑,
@dynamic delegate;


- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
            UIButton *button = [[UIButton alloc]init];
        
            //设置背景图片
            [button setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        
            //设置图片
            [button setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        
            [button setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        
        
            [button addTarget:self action:@selector(plusButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
            //设置大小
            button.size = button.currentBackgroundImage.size;

            [self addSubview:button];
        
            self.plusButton = button;
        
    }
    
    return self;
}

- (void)plusButtonClick:(UIButton *)sender{
    
    if ([self.delegate respondsToSelector:@selector(tabBar:plusButton:)]) {
        [self.delegate tabBar:self plusButton:sender];
    }
    
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    //调整加好按钮的位置
    self.plusButton.centerX = self.width * 0.5;
    self.plusButton.centerY = self.height * 0.5;

    //取出子控件的个数
    NSInteger count = self.subviews.count;
    NSInteger index = 0;
    //计算每一个 button 的位置
    CGFloat buttonWidth = self.width * 0.2;
    
    for (int i = 0; i< count; i++) {
        
        UIView *childView = self.subviews[i];
        //判断是否是 UITabBarButton
        if ([childView isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            //设置宽度和 x 值
            childView.width = buttonWidth;
            //这里乘以的是自己维护的 index, 因为还有其他的子控件,所以不能用 i,
            childView.x = buttonWidth * index;
            
            index++;
            
            if (index == 2) {
                index++;
            }
        }
    }
}




@end
